package com.cib.xagcjs.utils.gm;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SM4Utils
{

	public SM4Utils()
	{
	}
public static String encryptData_ECB(String plainText, String secretKey, boolean hexString, String code,boolean b){
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_ENCRYPT;

			byte[] keyBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
			}
			else
			{
				keyBytes = secretKey.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes(code));
			//System.out.println(Arrays.toString(encrypted));
			String cipherText = new BASE64Encoder().encode(encrypted);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Pattern p = Pattern.compile("\\s*|\t|\r|\n");
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
}
	public  String encryptData_ECB(String plainText, String secretKey, boolean hexString, String code)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_ENCRYPT;

			byte[] keyBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
			}
			else
			{
				keyBytes = secretKey.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes(code));
			String cipherText = new BASE64Encoder().encode(encrypted);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Pattern p = Pattern.compile("\\s*|\t|\r|\n");
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public static byte[] encryptData_ECB(byte[] plainText,byte[] secretKey)
{
	try
	{
		SM4_Context ctx = new SM4_Context();
		ctx.isPadding = true;
		ctx.mode = SM4.SM4_ENCRYPT;

		SM4 sm4 = new SM4();
		sm4.sm4_setkey_enc(ctx, secretKey);
		byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText);
		return encrypted;
	}
	catch (Exception e)
	{
		e.printStackTrace();
		return null;
	}
}
	public static byte[] encryptData_ECB1(byte[] plainText,byte[] secretKey)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = false;
			ctx.mode = SM4.SM4_ENCRYPT;

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, secretKey);
			byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText);
			return encrypted;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public static byte[] encryptData_ECB(byte[] plainText,byte[] secretKey,boolean pad)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = pad;
			ctx.mode = SM4.SM4_ENCRYPT;

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, secretKey);
			byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText);
			return encrypted;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public static String decryptData_ECB(String cipherText,String secretKey,boolean hexString,String code,boolean b){
		{
			try
			{
				SM4_Context ctx = new SM4_Context();
				ctx.isPadding = true;
				ctx.mode = SM4.SM4_DECRYPT;

				byte[] keyBytes;
				if (hexString)
				{
					keyBytes = Util.hexStringToBytes(secretKey);
				}
				else
				{
					keyBytes = secretKey.getBytes();
				}

				SM4 sm4 = new SM4();
				sm4.sm4_setkey_dec(ctx, keyBytes);
				byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText));
				return new String(decrypted, code);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				return null;
			}
		}
	}
	public String decryptData_ECB(String cipherText,String secretKey,boolean hexString,String code)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_DECRYPT;

			byte[] keyBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
			}
			else
			{
				keyBytes = secretKey.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, keyBytes);
			byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText));
			return new String(decrypted, code);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static  byte[] decryptData_ECB(byte[] cipherText,byte[] secretKey)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_DECRYPT;

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, secretKey);
			byte[] decrypted = sm4.sm4_crypt_ecb(ctx, cipherText);
			return decrypted;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public static  byte[] decryptData_ECB1(byte[] cipherText,byte[] secretKey)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = false;
			ctx.mode = SM4.SM4_DECRYPT;

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, secretKey);
			byte[] decrypted = sm4.sm4_crypt_ecb(ctx, cipherText);
			return decrypted;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public static  byte[] decryptData_ECB(byte[] cipherText,byte[] secretKey,boolean pad)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = pad;
			ctx.mode = SM4.SM4_DECRYPT;

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, secretKey);
			byte[] decrypted = sm4.sm4_crypt_ecb(ctx, cipherText);
			return decrypted;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public String encryptData_CBC(String plainText,String secretKey,String iv,boolean hexString,String code) throws Exception
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_ENCRYPT;

			byte[] keyBytes=new byte[16];
			byte[] ivBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
				ivBytes = Util.hexStringToBytes(iv);
			}
			else
			{
				byte[] keykeyBytes = secretKey.getBytes("utf-8");
				int i=0;
				if(keykeyBytes.length>=16){
					i=16;
				}else{
					i=keykeyBytes.length;
				}
				for(int j=0;j<i;j++){
					keyBytes[j]=keykeyBytes[j];
				}
				ivBytes = iv.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes(code));
			String cipherText = new BASE64Encoder().encode(encrypted);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Pattern p = Pattern.compile("\\s*|\t|\r|\n");
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public byte[] encryptData_CBC(byte[] plainText,byte[] secretKey,byte[] iv,boolean pad) throws Exception
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = pad;
			ctx.mode = SM4.SM4_ENCRYPT;



			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, secretKey);
			byte[] encrypted = sm4.sm4_crypt_cbc(ctx, iv, plainText);
			return encrypted;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public String decryptData_CBC(String cipherText, String secretKey,String iv,boolean hexString,String code) throws Exception
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_DECRYPT;

			byte[] keyBytes=new byte[16];
			byte[] ivBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
				ivBytes = Util.hexStringToBytes(iv);
			}
			else
			{
				byte[] keykeyBytes = secretKey.getBytes("utf-8");
				int i=0;
				if(keykeyBytes.length>=16){
					i=16;
				}else{
					i=keykeyBytes.length;
				}
				for(int j=0;j<i;j++){
					keyBytes[j]=keykeyBytes[j];
				}
				ivBytes = iv.getBytes();
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, keyBytes);
			byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText));
			return new String(decrypted, code);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	@SuppressWarnings("unused")
	public byte[] decryptData_CBC(byte[] cipherText, byte[] secretKey,byte[] iv,boolean pad) throws Exception
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = pad;
			ctx.mode = SM4.SM4_DECRYPT;

			byte[] ivBytes;
			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, secretKey);
			byte[] decrypted = sm4.sm4_crypt_cbc(ctx, iv, cipherText);
			return decrypted;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
//	public static void main(String[] args) throws IOException
//	{
//		try{
//			String plainText = "{\"persId\":\"32082119941005373X\",\"persIdTp\":\"01\",\"nm\":\"朱宇星\\"}";
//
//			SM4Utils sm4 = new SM4Utils();
//			String secretKey = "abcdedfg12345678abcdedfg12345678";		 ///
//			boolean hexString = false;
//
//
//			String cipherText = sm4.encryptData_ECB(plainText,secretKey,hexString,"utf-8");
//			System.out.println("密文: " + cipherText);
//			System.out.println("");
//
//			plainText = sm4.decryptData_ECB(cipherText,secretKey,hexString,"utf-8");
//			System.out.println("明文: " + plainText);
//		} catch(Exception e){
//			e.printStackTrace();
//		}
//	}
}
